package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult getSetMealList(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.getSetMealList(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void addSeatmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.addSeatmeal(setmeal);

        Long setmealId = setmeal.getId();

        List<SetmealDish> SetmealDishes = setmealDTO.getSetmealDishes();
        if (SetmealDishes.size() > 0 && SetmealDishes != null) {
            for (SetmealDish setmealDish : SetmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
        }

        setmealDishMapper.addSetmealDish(SetmealDishes);
    }

    @Override
    public void setStatus(Short status, Integer id) {
        setmealMapper.setStatus(id, status);
    }

    @Override
    public void deleteSetmeal(String ids) {
        setmealMapper.deleteSetmeal(ids);
    }

    @Override
    public SetmealVO getSetmeal(Integer id) {
        Setmeal setmeal = setmealMapper.getSetmeal(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        setmealVO.setSetmealDishes(setmealDishes);
        BeanUtils.copyProperties(setmeal, setmealVO);
        return setmealVO;
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateSetmeal(setmeal);

        Long setmealId = setmeal.getId();
        List<SetmealDish> SetmealDishes = setmealDTO.getSetmealDishes();
        if (SetmealDishes.size() > 0 && SetmealDishes != null) {
            setmealDishMapper.deleteSetmealDish(setmealId);
            for (SetmealDish setmealDish : SetmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
        }
        setmealDishMapper.addSetmealDish(SetmealDishes);
    }

    @Override
    public List<SetmealVO> setmealList(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.setmealList(setmeal);

        List<SetmealVO> setmealVOS = new ArrayList<>();
        for (Setmeal setmeal1 : list) {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal1, setmealVO);
            setmealVOS.add(setmealVO);
        }
        return setmealVOS;
    }

    @Override
    public List<DishItemVO> getdishItemListBySetmealId(Integer setmealId) {
      List<DishItemVO> dishItemVOLish=  setmealMapper.getDishBySetmealDishId( setmealId);
        return dishItemVOLish;
    }
}
